package com.community.util;

import com.community.bean.User;
import com.community.entity.Student;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

public class SessionUtil {

    public static final String ADMIN = "user";
    public static final String STUDENT = "student";

    public static Integer getCurUserId(){
       User user = getCurAdmin();
       if(user!=null){
           return user.getId();
       }
       return null;
    }

    public static String getCurStudentId(){
        Student student = getCurStudent();
        if(student!=null){
            return student.getId();
        }
        return null;
    }

    public static HttpSession getSession(){
        ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        if (attributes == null) {
            return null;
        }
        HttpServletRequest request =attributes.getRequest();
        return request.getSession();
    }
    public static User getCurAdmin(){
        HttpSession session = getSession();
        User user =  (User)session.getAttribute(ADMIN);
        String sessionId = session.getId();
        if(user != null && sessionId.equals(session.getServletContext().getAttribute(user.getId().toString())) ){
            return user;
        }
         return null;
        //  user = new User();user.setId(Constants.ADMIN_USER_ID);   return user;
    }

    public static Student getCurStudent(){
        HttpSession session = getSession();
        Student student =  (Student)session.getAttribute(STUDENT);
        String sessionId = session.getId();
        if(student != null && sessionId.equals(session.getServletContext().getAttribute(student.getId())) ){
            return student;
        }
        if(true){
            return null;
        }else{
            student = new Student();student.setId("402883475fa904a6015fa905529e0001");
            student.setUsername("17754415587");
            student.setVip_level(0);
            return student;
        }
    }

    public static void adminLogin(User user) {
        HttpSession session = getSession();
        session.setAttribute(ADMIN,user);
        String sessionId = session.getId();
        ServletContext applicationCtx = session.getServletContext();
        applicationCtx.setAttribute(user.getId().toString(), sessionId);
    }

    public static void adminLogout() {
        HttpSession session = getSession();
        User user =  (User)session.getAttribute(ADMIN);
        if(user !=null){
            ServletContext applicationCtx = session.getServletContext();
            applicationCtx.removeAttribute(user.getId().toString());
            session.removeAttribute("user");
        }
    }

    public static void studentLogin(Student student) {
        HttpSession session = getSession();
        session.setAttribute(STUDENT,student);
        String sessionId = session.getId();
        ServletContext applicationCtx = session.getServletContext();
        applicationCtx.setAttribute(student.getId(), sessionId);
    }

    public static void studentLogout() {
        HttpSession session = getSession();
        Student user = (Student)session.getAttribute(STUDENT);
        if(user !=null){
            ServletContext applicationCtx = session.getServletContext();
            applicationCtx.removeAttribute(user.getId());
            session.removeAttribute(STUDENT);
        }
    }
}
